// 权限表

const { removeRule } = require("../../controller/user/rule")
const Rule = require("../../model/user/rule")
const { getTime } = require("../../utils/gettime")

const {
    checkNull,
    isJson,
    handleNumber,

} = require("../../utils/verification")

function findOne(data) {
    let res = Rule.findOne({
        attributes: ["id", "rulename", "summary", "createdate", "creator", "update", "updator"],
        where: data
    })
    return res
}

// 根据 id 查询权限 
exports.getById = async ({ id }) => {
    try {
        if (!id) return { msg: "查询失败 id 为空", code: "400" }
        let res = await findOne({ id })
        if (res) {
            return { msg: "查询成功", code: "200", data: res }
        }
        return { msg: "查询失败", code: "400" }
    } catch (err) { return { msg: "查询失败", code: "400", err } }
}
// 获取权限列表    
exports.getList = async ({ pageNum, pageSize }) => {
    try {
        let checkedNum = handleNumber(pageNum, "pageNum")
        if (checkedNum.code !== "200") return checkedNum
        let checkedSize = handleNumber(pageSize, "pageSize")
        if (checkedSize.code !== "200") return checkedSize
        if (!isNaN(pageNum) && !isNaN(pageSize)) {
            let res = await Rule.findAndCountAll({
                offset: pageNum * pageSize - pageSize,
                limit: pageSize,
                attributes: ["id", "rulename", "summary", "creator", "createdate"]
            })
            if (res) return { msg: "查询成功", code: "200", data: res }
            return { msg: "查询失败", code: "400" }
        } else {
            return { msg: "获取失败 pageNum or pageSize is undefind", code: "400" }
        }
    } catch (err) { return { msg: "查询失败", code: "400", err } }
}
// 添加权限    
exports.create = async ({ rulename, summary, creator }) => {
    try {
        if (!rulename) return { msg: "添加失败: 'rulename' is undefind", code: "400" }
        let isCreate = await Rule.create({ rulename, summary, creator, createdate: getTime() })
        if (!isCreate) return { msg: "添加失败", code: "400" }
        let res = await Rule.findOne({
            attributes: ["id", "rulename", "summary", "createdate", "creator"],
            where: { rulename }
        })
        return { msg: "添加成功", code: "200", data: res }
    } catch (err) { return { msg: "添加失败", code: "400", err } }
}
// 编辑权限
exports.edit = async ({ rulename, summary, updator, id }) => {
    try {
        if (!id) return { msg: "编辑失败 id is undefind", code: "400" }
        let newData = {}
        if (summary) newData.summary = summary;
        if (updator) newData.updator = updator;
        newData.update = getTime()
        let isUpdate = await Rule.update(newData, { where: { id } })
        if (isUpdate) {
            let res = await findOne({ id })
            if (isUpdate) {
                return { msg: "编辑成功", code: "200", data: res }
            } else {
                return { msg: "编辑失败", code: "400" }
            }
        } else {
            return { msg: "编辑失败", code: "400" }
        }
    } catch (err) { return { msg: "编辑失败", code: "400", err } }
}
// 删除权限
exports.remove = async ({ id }) => {
    try {
        if (!id) return { msg: "删除失败", code: "400" }
        let res = await Rule.destroy({ where: { id } })
        if (res) return { msg: "删除成功", code: "200" }
        return { msg: "删除失败", code: "400" }
    } catch (err) { return { msg: "删除失败", code: "400", err } }
}